#include "VS3DTexture.h"
#include "VSGraphicInclude.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VS3DTexture,VSTexture)
static bool gs_bStreamRegistered_VS3DTexture= VS3DTexture::RegisterMainFactory ();
bool VS3DTexture::ms_bRegisterMainFactory = false;
bool VS3DTexture::RegisterMainFactory()
{
	if (!ms_bRegisterMainFactory)
	{ 
		VSMain::AddInitialFuntion(VS3DTexture::InitialClassFactory); 
		ms_bRegisterMainFactory = true;
	} 
	return ms_bRegisterMainFactory;

}
bool VS3DTexture::InitialClassFactory()
{
	ms_ClassFactory.AddElement(ms_Type.GetName(),FactoryFunc);
	
	return 1;
}
VSObject * VS3DTexture::FactoryFunc()
{
	return VS_NEW VS3DTexture();

}
VS3DTexture::VS3DTexture(unsigned int uiWidth, unsigned int uiHeight,unsigned int uiLength,
						 unsigned int uiFormatType,unsigned int uiMipLevel,bool bIsStatic)
						 :VSTexture(uiFormatType,uiWidth,uiHeight,uiLength,uiMipLevel,bIsStatic)
{

	SetMipLevel();

}
VS3DTexture::~VS3DTexture()
{

}
VS3DTexture::VS3DTexture()
{

}
void VS3DTexture::SetMipLevel()
{
	VSMAC_ASSERT(m_uiWidth && m_uiHeight);
	VSMAC_ASSERT(IsTwoPower(m_uiWidth) && IsTwoPower(m_uiHeight) && IsTwoPower(m_uiLenght));
	unsigned int uiWidthLevel;
	unsigned int uiWidth = m_uiWidth;
	_asm bsr eax, uiWidth
	_asm mov uiWidthLevel, eax
	
	
	unsigned int uHeightLevel;
	unsigned int uiHeight = m_uiHeight;
	_asm bsr eax, uiHeight
	_asm mov uHeightLevel, eax

	unsigned int uiLengthLevel;
	unsigned int uiLength = m_uiLenght;
	_asm bsr eax, uiLength
	_asm mov uiLengthLevel, eax

	unsigned int uiCurMipLevel = Max(Max(uiWidthLevel,uHeightLevel),uiLengthLevel) + 1;
	if (!m_uiMipLevel || uiCurMipLevel < m_uiMipLevel)
	{
		m_uiMipLevel = uiCurMipLevel;
	}

}